home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 October: Mac OS SDK / Dev.CD Oct 97 SDK1.toast / Development Kits (Disc 1) / QuickDraw 3D / RAVE SDK 1.5 MacOS / RaveDemo / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-14  |  6.8 KB  |  320 lines  |  [TEXT/CWIE]

  1.  
  2. #include "MyHeader.h"
  3.  
  4. // static function prototypes
  5. static void HandleEvent(EventRecord * event);
  6. static void HandleMenu(long selection);
  7. static void HandleQuit(void);
  8. static void HandleKey(EventRecord * event);
  9. static void PerFrameKeyCheck(void);
  10.  
  11. GlobalState globalState;
  12.  
  13. // code
  14. void main(void)
  15. {
  16.     EventRecord theEvent;
  17.  
  18.     /*****/
  19.  
  20.     InitGraf(&qd.thePort);
  21.     InitFonts();
  22.     InitWindows();
  23.     InitMenus();
  24.     TEInit();
  25.     InitDialogs(nil);
  26.     InitCursor();
  27.  
  28.     SetMenuBar(GetNewMBar(menuBarID));
  29.     AppendResMenu(GetMenuHandle(appleMenuID), 'DRVR');
  30.     DrawMenuBar();
  31.     
  32.     MyTestSetup();
  33.     
  34.  
  35.     do{
  36.         WaitNextEvent(everyEvent, &theEvent, 0, NULL);
  37.         HandleEvent(&theEvent);
  38.     } while (1);
  39. }
  40.  
  41.  
  42.  
  43. static void HandleEvent(EventRecord * event)
  44. {
  45.     WindowPtr theWindow;
  46.     static Rect growRect = {100, 100, 32000, 32000};
  47.     long newWindowSize;
  48.     
  49.     /***********/
  50.  
  51.     switch(event->what){
  52.     case autoKey:
  53.     case keyDown:
  54.         if(event->modifiers & cmdKey){
  55.             HandleMenu(MenuKey(event->message & charCodeMask));
  56.         } else {
  57.             HandleKey(event);
  58.         }
  59.         break;
  60.     case mouseDown:
  61.         switch(FindWindow(event->where, &theWindow)){
  62.         case inMenuBar:
  63.             HandleMenu(MenuSelect(event->where));
  64.             break;
  65.         case inDrag:
  66.             DragWindow(theWindow, event->where, &(**GetGrayRgn()).rgnBBox);
  67.             WindowChanged(theWindow);
  68.             break;
  69.         case inGrow:
  70.             newWindowSize = GrowWindow(theWindow, event->where, &growRect);
  71.             SizeWindow(theWindow, LoWord(newWindowSize), HiWord(newWindowSize), true);
  72.             WindowChanged(theWindow);
  73.             break;
  74.         case inContent:
  75.             SelectWindow(theWindow);
  76.             break;
  77.         case inGoAway:
  78.             if(TrackGoAway(theWindow, event->where)){
  79.                 ClosingWindow(theWindow);
  80.                 CloseWindow(theWindow);
  81.             }
  82.             break;
  83.         }
  84.         break;
  85.     case updateEvt:
  86.         theWindow = (WindowPtr)event->message;
  87.         BeginUpdate(theWindow);
  88.         EndUpdate(theWindow);
  89.         WindowChanged(theWindow); // this is incase the bit depth changed
  90.         DrawFrame((MyState*)GetWRefCon(theWindow));
  91.         break;
  92.     case activateEvt:
  93.         theWindow = (WindowPtr)event->message;
  94.         
  95.         if(event->modifiers & activeFlag){
  96.             UpdateMyMenus(theWindow);
  97.         }
  98.  
  99.         break;
  100.     case nullEvent:
  101.         PerFrameKeyCheck();
  102.         theWindow = FrontWindow();
  103.         while(theWindow){
  104.             MyState * state = (MyState*)GetWRefCon(theWindow);
  105.             if(state->active)
  106.                 DrawFrame(state);
  107.             
  108.             theWindow = (WindowPtr)((WindowPeek)theWindow)->nextWindow;
  109.         }
  110.         break;
  111.     }
  112. }
  113.  
  114.  
  115. static void HandleMenu(long selection)
  116. {
  117.     short menuID = selection >> 16;
  118.     short itemID = selection;
  119.     WindowPtr w;
  120.     
  121.     Str255 itemName;
  122.  
  123.     ///////////
  124.     
  125.     if(menuID == appleMenuID && itemID > 2){
  126.         GetMenuItemText(GetMenuHandle(appleMenuID), itemID, itemName);
  127.         OpenDeskAcc(itemName);
  128.     }
  129.  
  130.  
  131.     switch(menuID){
  132.     case fileMenuID:
  133.         switch(itemID){
  134.         case 1:
  135.             SetupNewWindow();
  136.             break;
  137.         case 3:
  138.             w = FrontWindow();
  139.             if(w){
  140.                 ClosingWindow(w);
  141.                 CloseWindow(w);
  142.             }
  143.             break;
  144.         case 6:
  145.             HandleQuit();
  146.             break;
  147.         }
  148.         break;
  149.     case engineMenuID:
  150.     case optionsMenuID:
  151.     case antialiasingMenuID:
  152.     case textureFilterMenuID:
  153.         HandleOtherMenu(menuID, itemID);
  154.         break;
  155.     }
  156.     
  157.     HiliteMenu(0);
  158. }
  159.  
  160. static void HandleQuit(void)
  161. {
  162.     WindowPtr w;
  163.     
  164.     /********/
  165.     
  166.     while(w = FrontWindow()){
  167.         ClosingWindow(w);
  168.         CloseWindow(w);
  169.     }
  170.  
  171.     UnloadTextures();
  172.  
  173.     ExitToShell();
  174. }
  175.  
  176.  
  177.  
  178. #define rotatePerKey 0.1
  179. #define movePerKey 0.5
  180.  
  181. static void HandleKey(EventRecord * event)
  182. {
  183. //    MyMatrix m;
  184.  
  185.     unsigned char theKey = (event->message & keyCodeMask) >> 8;
  186.     unsigned char theChar = event->message & charCodeMask;
  187.     switch(theChar){
  188.     case charESC:
  189.         globalState.stopObjects = !globalState.stopObjects;
  190.         break;
  191. /*
  192.     case charUp:
  193.         globalState.camera.w.x += globalState.camera.z.x * movePerKey;
  194.         globalState.camera.w.y += globalState.camera.z.y * movePerKey;
  195.         globalState.camera.w.z += globalState.camera.z.z * movePerKey;
  196.         break;
  197.     case charDown:
  198.         globalState.camera.w.x += globalState.camera.z.x * -movePerKey;
  199.         globalState.camera.w.y += globalState.camera.z.y * -movePerKey;
  200.         globalState.camera.w.z += globalState.camera.z.z * -movePerKey;
  201.         break;
  202.     case charLeft:
  203.         MyMatrixSetRotateY(rotatePerKey, &m);
  204.         MyMatrixRotateByMatrix(&globalState.camera, &m);
  205.         break;
  206.     case charRight:
  207.         MyMatrixSetRotateY(-rotatePerKey, &m);
  208.         MyMatrixRotateByMatrix(&globalState.camera, &m);
  209.         break;
  210. */
  211.     default:
  212.         globalState.unusedKey = theChar;
  213.     }
  214.     globalState.unusedKey = theKey;
  215. }
  216.  
  217.  
  218. #define IsKeyDown(keyMap, theKey) (((unsigned char *)(keyMap))[(theKey) / 8] & 1 << ((theKey) % 8))
  219.  
  220. #define keyLeft        0x7b
  221. #define keyRight    0x7c
  222. #define keyUp        0x7e
  223. #define keyDown        0x7d
  224. #define keyCommand    55
  225. #define keyOption    58
  226. #define keyShift    56
  227. #define keyControl    63
  228. #define keySpace    0x31
  229.  
  230.  
  231. static void PerFrameKeyCheck(void)
  232. {
  233.     KeyMap theKeys;
  234.     MyMatrix m, m2;
  235.     
  236.     int command, option;
  237.  
  238.     /*********/
  239.  
  240.     GetKeys(theKeys);
  241.  
  242.     command = IsKeyDown(theKeys, keyCommand);
  243.     option = IsKeyDown(theKeys, keyOption);
  244.     
  245.     if(IsKeyDown(theKeys, keySpace)){
  246.         globalState.roll = 0;
  247.         globalState.pitch = 0;
  248.         globalState.yaw = 0;
  249.         MyMatrixClear(&globalState.camera);
  250.     }
  251.     if(IsKeyDown(theKeys, keyLeft)){
  252.         if(command){
  253.             globalState.camera.w.x += globalState.camera.x.x * movePerKey;
  254.             globalState.camera.w.y += globalState.camera.x.y * movePerKey;
  255.             globalState.camera.w.z += globalState.camera.x.z * movePerKey;
  256.         } else if (option) {
  257.             globalState.roll -= rotatePerKey;
  258.         } else {
  259.             globalState.yaw += rotatePerKey;
  260.         }
  261.     }
  262.  
  263.     if(IsKeyDown(theKeys, keyRight)){
  264.         if(command){
  265.             globalState.camera.w.x += globalState.camera.x.x * -movePerKey;
  266.             globalState.camera.w.y += globalState.camera.x.y * -movePerKey;
  267.             globalState.camera.w.z += globalState.camera.x.z * -movePerKey;
  268.         } else if (option) {
  269.             globalState.roll += rotatePerKey;
  270.         } else {
  271.             globalState.yaw -= rotatePerKey;
  272.         }
  273.     }
  274.  
  275.     if(IsKeyDown(theKeys, keyUp)){
  276.         if(command){
  277.             globalState.camera.w.x += globalState.camera.y.x * movePerKey;
  278.             globalState.camera.w.y += globalState.camera.y.y * movePerKey;
  279.             globalState.camera.w.z += globalState.camera.y.z * movePerKey;
  280.         } else if (option) {
  281.             globalState.pitch -= rotatePerKey;
  282.         } else {
  283.             globalState.camera.w.x += globalState.camera.z.x * movePerKey;
  284.             globalState.camera.w.y += globalState.camera.z.y * movePerKey;
  285.             globalState.camera.w.z += globalState.camera.z.z * movePerKey;
  286.         }
  287.     }
  288.  
  289.     if(IsKeyDown(theKeys, keyDown)){
  290.         if(command){
  291.             globalState.camera.w.x += globalState.camera.y.x * -movePerKey;
  292.             globalState.camera.w.y += globalState.camera.y.y * -movePerKey;
  293.             globalState.camera.w.z += globalState.camera.y.z * -movePerKey;
  294.         } else if (option) {
  295.             globalState.pitch += rotatePerKey;
  296.         } else {
  297.             globalState.camera.w.x += globalState.camera.z.x * -movePerKey;
  298.             globalState.camera.w.y += globalState.camera.z.y * -movePerKey;
  299.             globalState.camera.w.z += globalState.camera.z.z * -movePerKey;
  300.         }
  301.     }
  302.  
  303.     MyMatrixSetRotateZ(globalState.roll, &m);
  304.  
  305.     MyMatrixSetRotateX(globalState.pitch, &m2);
  306.     MyMatrixRotateByMatrix(&m, &m2);
  307.  
  308.     MyMatrixSetRotateY(globalState.yaw, &m2);
  309.     MyMatrixRotateByMatrix(&m, &m2);
  310.  
  311.     globalState.camera.x = m.x;
  312.     globalState.camera.y = m.y;
  313.     globalState.camera.z = m.z;
  314.  
  315.     
  316.  
  317. }
  318.  
  319.  
  320.